import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

let defaultaddr = '16级信本02班'
let defaulttotal = '42'
let defaultteacher = '徐红丹'
let defaulttphone = '111110000' 
let defaultusername = ''
try {
    if (localStorage.addr) {
        defaultaddr = localStorage.addr
        defaulttotal = localStorage.total
        defaultteacher = localStorage.teacher
        defaulttphone = localStorage.tphone
        defaultusername = localStorage.tphone
    }
} catch (error) {}


export default new Vuex.Store({
    state:{
        addr:defaultaddr,
        total:defaulttotal,
        teacher:defaultteacher,
        tphone:defaulttphone,
        username:defaultusername
    },
    actions:{
        changeclass (ctx,item){
            ctx.commit('changeclass',item)
        },
        changeusername(ctx,res){
            ctx.commit('changeusername',res)
        }
    },
    mutations:{
        changeclass(state,item){
            state.addr = item.addr
            state.total = item.total
            state.teacher = item.teacher
            state.tphone = item.tphone
            try {
                localStorage.class = item.addr
                localStorage.total = item.total
                localStorage.teacher = item.teacher
                localStorage.tphone = item.tphone
            } catch (error) {}
           
        },
        changeusername(state,res){
            state.username = res
            try{
                localStorage.username = res
            } catch(error){}
        }
    }
})